package com.example.qgorderconsoumer.controller;

import com.example.qgcommon.util.RedisUtil;
import com.example.qgcommon.util.ReturnUtil;
import com.example.qgorderconsoumer.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private OrderService service;

    @RequestMapping("/getOrdersByUid")
    public Map getOrdersByUid(@RequestParam("token") String token) throws JsonProcessingException {
        String userJson = redisUtil.getStr( "token:" +token);
        Map user = new ObjectMapper().readValue(userJson,Map.class);
        //获得用户id
        int userId = (Integer) user.get( "id" );
        List<Map> list = service.getOrdersByUid( userId );
        return ReturnUtil.success(list);
    }

    @RequestMapping("/getById")
    public Map getById(@RequestParam("id") String id){
        Map map = service.getById( id );
        return ReturnUtil.success(map);
    }

}
